
=begin
  The GadgetViewerController services a sample container application that can be used
  for testing gadgets and the ruby-gadgets server.  The UI that this serves is not meant
  to be a part of the ruby-gadgets server, however it can serve as a starting point for 
  your container integration effort.
=end
class GadgetViewerController < ApplicationController
  
  def initialize
    super
    @gadget_server = 'http://localhost:3002';
  end
  
  
  # Renders the Gadget Viewer home page
  def index
    @urls = build_spec_urls(Gadget.get_local_spec_filenames)
  end
  
  
  # Used to render the metadata in an erb template
  def display_metadata
    json = params[:json]
    @json = CGI::unescape(json)
    param_str = Crack::JSON.parse(@json)
    @metadata = param_str['gadgets'][0]
    spec = XmlSpec.new()
    spec.read(params[:spec_url])
    @xml_spec = spec.source
    @xml_spec_contents = spec.contents
    @xml_spec_style = spec.get_style_for_view('default')
  end
  
  
  private  
  
  def build_spec_urls(files)
    urls = []
    files.each do |file|
      urls.push(@gadget_server + file.sub("public", ''))
    end
    return urls
  end
  
end
